package homework.no_5;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class UDPSender {
    public static void main(String[] args) {
        DatagramSocket socket = null;
        Scanner sc = new Scanner(System.in);
        String str = null;


        while (true) {
            try {
                socket = new DatagramSocket(9000);
                byte[] bytes = new byte[1024];
                DatagramPacket packet = new DatagramPacket(bytes,bytes.length);
                socket.receive(packet);

                System.out.println("客户意见：");
                str = new String(packet.getData(),0,packet.getLength());
                System.out.println(str);
                if ("bye".equalsIgnoreCase(str)){
                    System.out.println("感谢你的建议，欢迎下次咨询！");
                    break;
                }

                str = sc.nextLine();
                byte[] b = str.getBytes();
                DatagramPacket packet1 = new DatagramPacket(b, b.length,
                        InetAddress.getLocalHost(), 9000);
                socket.send(packet1);

            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}
